from django.shortcuts import render,redirect
from datetime import datetime
from web.models import Category,Topic
# Create your views here.

def index1(request):
    ctx = {"data": "<p>这是一个段落</p>",
           "l1": [1, 2, 3, 45],
           "l2": [5, 6, 7, 89],
           "now": datetime.now(),
           "username": "",
           "password": None,
           "title": "啥地方看假发是打开工具发啥快递更加爱你山东科技馆那思考的结果那山东科技馆纳斯达克就好啦多久",
           "title2": '<p>啊水电费就发生第广发司空见惯发啥快递减肥法可视对讲海蛤壳收到就好</p>',
           }
    return render(request, "web/index1.html", context=ctx)


def index(request):
    ctx = {
        "navs": Category.objects.filter(is_nav=True).order_by("orders"),
        "xxxw": Topic.objects.filter(category__id=9).order_by("-release_date")[:5],
        "txgg": Topic.objects.filter(category__id=10).order_by("-release_date")[:8],
        "tpzt": Topic.objects.filter(category__id=11).order_by("-release_date")[:3],
    }
    return render(request, "web/index.html", context=ctx)


def show(request, id):
    return render(request, "web/show.html", context={"id": id,"d1": datetime(2022,5,31,17,25,30)})


def category_list(request):
    ctx = {"cs": Category.objects.all().order_by("id")}
    return render(request, "category/list.html", context=ctx)


def category_add(request):
    if request.method == "GET":
        ctx = {
            "types": get_types(),
            "ps": Category.objects.all()
        }
        return render(request, "category/add.html", context=ctx)
    elif request.method == "POST":
        c = Category()
        init_category(c, request)
        pid = int(request.POST.get("pid"))
        if pid != 0:
            c.parent = Category.objects.get(id=pid)
        c.save()
        return redirect("category.list")


def category_update(request, id):
    if request.method == "GET":
        ctx = {
            "types": get_types(),
            "ps": Category.objects.exclude(id=id),
            "c": Category.objects.get(id=id)
        }
        return render(request, "category/update.html", context=ctx)
    elif request.method == "POST":
        c = Category.objects.get(id=id)
        init_category(c, request)
        pid = int(request.POST.get("pid"))
        if pid != 0:
            c.parent = Category.objects.get(id=pid)
        else:
            c.parent = None
        c.save()
        return redirect("category.list")


def init_category(c, request):
    c.name = request.POST.get("name")
    c.type = int(request.POST.get("type"))
    c.is_nav = int(request.POST.get("is_nav"))

    c.is_index = int(request.POST.get("is_index"))
    c.href = request.POST.get("href")
    orders = request.POST.get("orders")
    if orders == "":
        orders = 9999
    else:
        orders = int(orders)
    c.orders = orders



def category_delete(request,id):
    c = Category.objects.get(id=id)
    c.delete()
    return redirect("category.list")


def get_types():
    types = [
        {"id": 0, "name": "导航栏目"},
        {"id": 1, "name": "文章列表栏目"},
        {"id": 2, "name": "内容栏目"},
        {"id": 3, "name": "图片栏目"},
        {"id": 4, "name": "超链接"},
    ]
    return types


def topic_list(request):
    ctx = {
        "ts": Topic.objects.all().order_by("-release_date")
    }
    return render(request, "topic/list.html", context=ctx)


def topic_add(request):
    if request.method == "GET":
        ctx = {
            "cs": Category.objects.filter(type__in=[1, 2, 3]) #得到所有的可以添加文章的栏目
        }
        return render(request, "topic/add.html", context = ctx)
    elif request.method == "POST":
        t = Topic()
        init_topic(t, request)
        t.save()
        return redirect("topic.list")


def topic_delete(request, id):
    t = Topic.objects.get(id=id)
    t.delete()
    return redirect("topic.list")


def topic_update(request, id):
    if request.method == "GET":
        ctx = {
            "t": Topic.objects.get(id=id),
            "cs": Category.objects.filter(type__in=[1, 2, 3])
        }
        return render(request, "topic/update.html", context=ctx)
    elif request.method == "POST":
        t = Topic.objects.get(id=id)
        init_topic(t, request)
        t.save()
        return redirect("topic.list")


def topic_show(request,id):
    ctx = {
        "t": Topic.objects.get(id=id)
    }
    return render(request, "topic/show.html", context=ctx)


def init_topic(t, request):
    t.title = request.POST.get("title")
    t.content = request.POST.get("content")
    t.release_date = request.POST.get("release_date")
    t.author = request.POST.get("author")
    t.category = Category.objects.get(id=int(request.POST.get("cid")))


def topic_web_show(request, id):
    ctx = {
        "t": Topic.objects.get(id=id),
        "navs": Category.objects.filter(is_nav=True).order_by("orders"),
    }
    return render(request, "web/topic.html", context=ctx)


def category_web_show(request, id):
    c = Category.objects.get(id=id)
    ts = None
    t = None
    if c.type == 0:
        #应该获取所有子栏目
        cs = Category.objects.filter(parent_id=c.id).order_by("orders")
        print(cs)
        #获取第一条子栏目
        show_c = cs[0]
    else:
        #显示子栏目时的操作
        if c.parent != None:
            #说明有父栏目,非独立子栏目
            cs = Category.objects.filter(parent_id=c.parent.id).order_by("orders")
        else:
            #独立子栏目
            cs = [c,]
        show_c = c
    if show_c.type==1:
        #表示是文章列表栏目
        ts = Topic.objects.filter(category_id=show_c.id)
    elif show_c.type==2:
        #内容栏目
        t = Topic.objects.filter(category_id=show_c.id)[0]
    elif show_c.type==3:
        #图片栏目
        pass
    ctx = {
        "cs": cs,
        "show_c": show_c,
        "ts": ts,
        "t": t,
        "navs": Category.objects.filter(is_nav=True).order_by("orders"),
    }
    return render(request, "web/category.html", context=ctx)
